package com.ukao.cashregister.printer;

import android.os.RemoteException;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gprinter.command.EscCommand;
import com.gprinterio.GpDevice;
import com.ukao.cashregister.bean.BatchlTabBean;
import com.ukao.cashregister.bean.ReceiptBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.MyDateUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.Utils;

import java.util.List;
import java.util.Vector;

import woyou.aidlservice.jiuiv5.IWoyouService;

import static com.amap.api.col.x.e;
import static com.amap.api.col.x.j;
import static com.ukao.cashregister.R.id.count;
import static com.ukao.cashregister.R.id.userName;

/**
 *打印小米的小票
 */

public    class PrintMiuiHelper {


    public   static   final   int   ALIGMENT_RIGHT=0;
    public   static   final   int   ALIGMENT_CENTER=1;
    public   static   final   int   ALIGMENT_LEFT=2;

    /**
     * 打印小票
     * @param woyouService
     * @param printInfo
     */
    public static   void receiptReceipt( IWoyouService woyouService,ReceiptBean printInfo){
        try {
            /*/*店名  与单号*/
            woyouService.setFontSize(40,null);/*字体大小*/
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_CENTER,null); /*对其方式*/
            woyouService.printText(printInfo.getMercName()+"\n",null); /*商户名字*/
            woyouService.printText(printInfo.getStoreName()+"\n",null);/*店名*/
            woyouService.setFontSize(25,null);/*字体大小*/
            woyouService.printText(printInfo.getOrderNo()+"\n",null);/*单号*/
            /*设置二维码*/
            woyouService.printBarCode(printInfo.getOrderNo(),6,115,3,0,null);/*128条码*/
            woyouService.lineWrap(1,null);
            List<ReceiptBean.OrderProductListBean> mProductList=printInfo.getOrderProductList();
            /*入库*/


            String[] storagtext = new String[2];
            storagtext[0]="入库："+MyDateUtils.formatDateCustom(printInfo.getRecoredTime(),"MM-dd HH:mm:ss");
            String    cloCount=printInfo.getClothCnt()+printInfo.getAnnexCnt();
            storagtext[1]=cloCount;
            //27
            int[] widthstory = new int[] { 24, 20};
            woyouService.printColumnsText(storagtext, widthstory, new int[] { 0, 2 },null);
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_RIGHT,null); /*对其方式*/
            String sendModeText=printInfo.getSendModeText();
            if (printInfo.getSendMode()==2&&!CheckUtils.isEmpty(printInfo.getSendWorkName())){
                sendModeText=sendModeText+":"+printInfo.getSendWorkName();

            }
            if (!CheckUtils.isEmpty(sendModeText)){
                woyouService.printText(sendModeText + "\n",null);
            }
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);

            /*衣物列表*/
            if (!CheckUtils.isEmpty(mProductList)) {
                Gson mGson = new Gson();
                for (int j = 0; j < mProductList.size(); j++) {

                    ReceiptBean.OrderProductListBean orderProductListBean = mProductList.get(j);
                    String color = "";
                    if (!CheckUtils.isEmpty(orderProductListBean.getColorDesc())) {  /*/*瑕疵*/
                        List<StringBean> mColorDesclist = mGson.fromJson(orderProductListBean.getColorDesc(),
                                new TypeToken<List<StringBean>>() {
                                }.getType());
                        StringBuffer mColorBuffer1 = new StringBuffer();
                        for (int i = 0; i < mColorDesclist.size(); i++) {
                            if (i != mColorDesclist.size() - 1) {
                                mColorBuffer1.append(mColorDesclist.get(i).getName() + "/");
                            } else {
                                mColorBuffer1.append(mColorDesclist.get(i).getName());
                            }


                        }
                        color = mColorBuffer1.toString();
                    }

                    String strflawDesc = "";
                    if (!CheckUtils.isEmpty(orderProductListBean.getFlawDesc())) {  /*/*瑕疵*/
                        List<StringBean> mflawDesclist = mGson.fromJson(orderProductListBean.getFlawDesc(),
                                new TypeToken<List<StringBean>>() {
                                }.getType());
                        StringBuffer mflawBuffer = new StringBuffer();
                        for (int i = 0; i < mflawDesclist.size(); i++) {
                            if (i != mflawDesclist.size() - 1) {
                                mflawBuffer.append(mflawDesclist.get(i).getName() + "/");
                            } else {
                                mflawBuffer.append(mflawDesclist.get(i).getName());
                            }


                        }
                        strflawDesc = mflawBuffer.toString();
                    }

                    String[] clothtext = new String[2];
                    clothtext[0]=orderProductListBean.getProductName() + "  " + orderProductListBean.getScanCode();
                    clothtext[1]="￥" + CheckUtils.isEmptyNumber(orderProductListBean.getSubtotal());
                    int[] width = new int[] { 27, 20};
                   woyouService.printColumnsText(clothtext, width, new int[] { 0, 2 },null);
                   //金额*打折和输出线
                    String[] strArray = new String[2];
                    strArray[0]="￥"+ CheckUtils.isEmptyNumber(orderProductListBean.getServiceAmt()) + "*" + orderProductListBean.getDiscount() + "%";
                    strArray[1]=orderProductListBean.getTranTypeStr()+" "+orderProductListBean.getTranNum();
                    woyouService.printColumnsText(strArray, width, new int[] { 0, 2 },null);
                    if (!CheckUtils.isEmpty(color)&&!CheckUtils.isEmpty(strflawDesc)){
                        color=color+"/"+strflawDesc;
                     }else if (CheckUtils.isEmpty(color)&&!CheckUtils.isEmpty(strflawDesc)){
                        color=strflawDesc;
                    }
                    Log.d("内容", "receiptReceipt: "+color);
                    Log.d("长度", "receiptReceipt: "+color.length());
                    if(color.length()>20){
                        color=color.substring(0,20);
                        color+="...";
                        woyouService.printText(color+"\n",null);
                    }else if(!color.isEmpty()){
                        woyouService.printText(color+"\n",null);
                    }
                }
            }
            if (!CheckUtils.isEmpty(mProductList)){
                woyouService.printText(PrintMiuiHelper.cutoffs(),null);
            }

            /*付款状态与价格*/
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_LEFT,null); /*对其方式*/
            woyouService.printText("衣物总额："+CheckUtils.isEmptyNumber(printInfo.getOrderPrice())+"\n",null);/*单号*/
            if (printInfo.getCouponAmt()!=0){/*优惠卷 */
                woyouService.printText("优惠卷："+CheckUtils.isEmptyNumber(printInfo.getCouponAmt())+"\n",null);

            }
            if (printInfo.getIsModifyPrice()){/*修改金额 */
                woyouService.printText("修改金额："+CheckUtils.isEmptyNumber(printInfo.getModifyPrice())+"\n",null);

            }
            int   takesEndFee=printInfo.getSendCarriage()+printInfo.getTakeCarriage();
            if (takesEndFee!=0){  /*取送费*/
                woyouService.printText("取送费："+CheckUtils.isEmptyNumber(takesEndFee)+"\n",null);
            }


            if (!CheckUtils.isNull(printInfo.getPaidPrice())&&printInfo.getPayStatus()){/* 已支付金额*/
                woyouService.printText("已付金额："+CheckUtils.isEmptyNumber(printInfo.getPaidPrice())+"\n",null);
            }
            woyouService.printText("付款状态："+printInfo.getPayStatusText()+"\n",null);
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_RIGHT,null); /*对其方式*/
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);

            /*客户备注*/
            if (!CheckUtils.isEmpty( printInfo.getUserRemark())){
                woyouService.printText("客户备注："+printInfo.getUserRemark()+"\n",null);
            }else{
                woyouService.printText("客户备注：无"+"\n",null);
            }
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);
            String  userName="";
            if (!CheckUtils.isEmpty(printInfo.getUserName())){
                userName="  "+printInfo.getUserName();
            }
            if (!CheckUtils.isEmpty(printInfo.getUserPhone())){
                userName=printInfo.getUserPhone()+userName;
            }
            woyouService.printText("客户："+userName+"\n",null);
            if(!CheckUtils.isEmpty(printInfo.getRfidNo())){
                woyouService.printText("卡号："+printInfo.getRfidNo()+"\n",null);
            }else if(!CheckUtils.isEmpty(printInfo.getUserCardNo())){
                woyouService.printText("卡号："+printInfo.getUserCardNo()+"\n",null);
            }
            String  discount="";
            if (printInfo.getDiscountType()){
                discount="多折扣";
            }else{
                discount=printInfo.getDiscount()+"%";
            }
           woyouService.printText("折扣："+discount+"  余额：￥"+CheckUtils.isEmptyNumber(printInfo.getUserBalance())+"\n",null);
            if (!CheckUtils.isEmpty(printInfo.getTakeAddress())){
                woyouService.printText(printInfo.getTakeAddress()+"\n",null);
            }
            woyouService.printText(PrintMiuiHelper.cutoffs(),null);
            if (!CheckUtils.isEmpty(printInfo.getWxCode())){
                woyouService.printText("微信号："+printInfo.getWxCode()+"\n",null);
            }
            if (!CheckUtils.isEmpty(printInfo.getServiceTel())){
                woyouService.printText("客服电话："+printInfo.getServiceTel()+"\n",null);
            }
            woyouService.printText("打印时间："+MyDateUtils.getDateTime()+"\n"+"\n",null);
            woyouService.setAlignment(PrintMiuiHelper.ALIGMENT_CENTER,null); /*对其方式*/
            woyouService.printQRCode(printInfo.getOrderNo(),5,2,null);/*二维码*/
            woyouService.lineWrap(1,null);
            woyouService.commitPrinterBuffer();





        }catch (Exception e){

        }


    }


    public static   String cutoffs(){
        StringBuffer  mStringBuffer=new StringBuffer();
        String  cutline= "____________________________________________";

        return mStringBuffer.append(cutline).toString()+"\n";
    }


    /**
     * 打印批号
     */
    public  static  void   batchPrinting(IWoyouService esc   , int  count, BatchlTabBean.ListBean printInfo){
        printGpBatch(esc,count, printInfo);


    }

    private static void printGpBatch(IWoyouService esc,int  count, BatchlTabBean.ListBean printInfo) {
    /*/*店名  与单号*/
        try {
            esc.setFontSize(45,null);/*字体大小*/
            esc.setAlignment(PrintMiuiHelper.ALIGMENT_CENTER,null); /*对其方式*/
            esc.printText("入厂袋号：" + printInfo.getCode() + "\n"+ "\n",null);
            esc.setFontSize(25,null);/*字体大小*/
            esc.printText("创建时间："+ MyDateUtils.formatDataTime(printInfo.getCreateTime()) ,null);
            esc.lineWrap(2,null);
            esc.printText("请扫码核对确认" + "\n",null);
            esc.lineWrap(1,null);
            esc.printQRCode(printInfo.getCode(),7,2,null);/*二维码*/
            esc.lineWrap(1,null);
            esc.setFontSize(40,null);/*字体大小*/
            int  size=printInfo.getProductCnt()+printInfo.getAnnexCnt();
            esc.printText("共" +size+ "件",null);
            esc.setFontSize(25,null);/*字体大小*/
            esc.lineWrap(2,null);
            esc.setAlignment(PrintMiuiHelper.ALIGMENT_RIGHT,null); /*对其方式*/
            String  msg="衣服"+printInfo.getProductCnt()+"件，附件"+printInfo.getAnnexCnt()+"件";
            esc.printText("确认签收：   "+msg,null);
            esc.lineWrap(2,null);
            esc.printText(cutoffs(),null);
            esc.lineWrap(1,null);
            if (!CheckUtils.isNull(printInfo.getStoreName())) {
                esc.printText(printInfo.getStoreName() + "\n",null);
            }
            esc.printText("地址：" +CheckUtils.isEmptyString(printInfo.getStoreRegionalDesc())+" "+CheckUtils.isEmptyString(printInfo.getStoreAddrDesc())+ "\n",null);
            esc.printText("打印时间：" + MyDateUtils.getDateTime() + "\n",null);
            esc.commitPrinterBuffer();
            if (count==0){
                printGpBatch(esc,1,printInfo);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


}
